package cc.rengu.igas.channel.cups.core.service.trans;

import cc.rengu.igas.channel.cups.common.constant.CupsParamConstant;
import cc.rengu.igas.channel.cups.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.cups.core.model.CupsFieldRspBean;
import cc.rengu.igas.channel.cups.core.service.base.BasePayService;
import cc.rengu.igas.channel.cups.facade.request.DepositConfirmRequest;
import cc.rengu.igas.channel.cups.facade.response.DepositConfirmResponse;
import cc.rengu.igas.share.facade.bean.CupsField;
import cc.rengu.igas.share.facade.bean.CupsFieldBean;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.BeanUtil;

/**
 * 功能描述：存款确认请求
 *
 * @author wangyongshuai
 * @date 2020/11/30
 */
public class CupsDepositConfirmService extends BasePayService<DepositConfirmRequest, DepositConfirmResponse> {
    @Override
    protected ChnlBizResponse callBizService(CupsFieldBean cupsFieldBean,
                                             DepositConfirmRequest depositConfirmRequest,
                                             DepositConfirmResponse depositConfirmResponse) throws Exception {
        //  请求
        CupsField cupsField = cupsFieldBean.getCupsField();
        BeanUtil.beanCopy(depositConfirmRequest, cupsField);
        cupsField.setOrderAmt(AmountUtil.changeY2F(cupsField.getOrderAmt()));
        cupsField.setMsgType("0220");
        cupsField.setProcCode("210000");
        cupsField.setConCode("00");

        //  终端读取能力
        //  IC卡条件代码
        //  小额免密标志
        //  终端类型
        //  特殊计费类型
        cupsFieldBean.createF60()
                .setF60_2_2(depositConfirmRequest.getTermRead())
                .setF60_2_3(depositConfirmRequest.getIcConCode())
                .setF60_2_6(depositConfirmRequest.getNoPinFlag())
                .setF60_2_5(depositConfirmRequest.getTermInfo().getDeviceType())
                .setF60_3_1(depositConfirmRequest.getSpecialBillType());
        cupsFieldBean.build();

        //  调用 CUPS   TODO 此交易配置暂时有误 ,关联了行内交易码, 后续解耦
        CupsFieldRspBean cupsFieldRspBean = callCups(cupsFieldBean, "P05MC001");

        // TODO   mac后续前面模块调用时怎么判??
//        depositConfirmResponse.setVerifyMacFlag(flag);
        // 应答赋值
        CupsField cupsFieldRsp = cupsFieldRspBean.getCupsFieldRsp();
        BeanUtil.beanCopy(cupsFieldRsp, depositConfirmResponse);
        depositConfirmResponse.setRespCode(cupsFieldRsp.getRespCode());
        depositConfirmResponse.setCupsRespCode(cupsFieldRsp.getRespCode());
        depositConfirmResponse.setSettleDate(cupsFieldRsp.getSettleDate());
        if (depositConfirmResponse.getSettleDate() != null) {
            depositConfirmResponse.setSettleDate(getCupsSettleDate(depositConfirmResponse.getSettleDate()));
        }
        if (CupsParamConstant.SUCCESS_CODE.equals(depositConfirmResponse.getRespCode())) {
            depositConfirmResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            depositConfirmResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
        } else {
            depositConfirmResponse.setRespCode(cupsFieldRspBean.getCupsFieldRsp().getRespCode());
        }
        return new ChnlBizResponse(depositConfirmResponse);
    }

}
